﻿@using A2.ERP.Models
@model A2.ERP.Models.t_Group

@{
    List<t_Group> groups = ViewBag.groups;
}

<section id="GroupManager">
    <div class="card">
        <div class="card-header">添加群组</div>
        <div class="card-body">
            @using (Html.BeginForm("GroupManager", "Setting", FormMethod.Post, new
            {
                @class = "addGroupFrm"
            }))
            {
                <div class="form-group">
                    <label>群组名称</label>
                    <input type="text" name="GroupName" value="" class="form-control" />
                </div>
                <div class="form-group">
                    <label>备注</label>
                    <input type="text" name="Description" value="" class="form-control" />
                </div>
                <div class="form-group">
                    <button class="btn btn-primary">添加</button>
                </div>
            }
        </div>
    </div>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>编号</th>
                <th>群名</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var group in groups)
            {
                <tr>
                    <td>@group.GroupID</td>
                    <td>@group.GroupName</td>
                    <td>@group.Description</td>
                    <td>
                        <button class="btn btn-sm btn-primary">
                            <a data-route="/Setting/EditGroup?id=@group.GroupID">
                                <i class="icon SkyUI icon-search"></i>
                                编辑
                            </a>
                        </button>

                        <button data-id="@group.GroupID" class="btn btn-sm btn-danger btn-delGroup" data-target="#delGroupModal" data-toggle="modal">
                            <i class="icon SkyUI icon-close"></i>
                            删除
                        </button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <div class="modal fade" id="delGroupModal" tabindex="-1" role="dialog" aria-labelledby="sysUserModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">确定要删除该权限组吗？</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    确定要删除该权限组吗?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-sureDelGroup">确定</button>
                </div>
            </div>
        </div>
    </div>
</section>


<script>
    var $addGroupFrm = $('#GroupManager .addGroupFrm');
    var $btnDelGroup = $('.btn-delGroup');
    var $btnSureDelGroup = $('.btn-sureDelGroup');

    var currentGroupId = null;

    $addGroupFrm.submit(function (e) {
        e.preventDefault();

        $addGroupFrm.ajaxSubmit({
            type: 'POST',
            dataType: 'json',
            success: function (e) {
                $tab.reloadTab('#GroupManager');
            }
        });
    });

    $btnDelGroup.on('click', function () {
        currentGroupId = $(this).attr('data-id');
    });

    $btnSureDelGroup.on('click', function () {
        $.ajax({
            url: '/setting/GroupManager',
            dataType: 'json',
            type: 'DELETE',
            data: { id: currentGroupId },
            success: function (data) {
                if (data.result) {
                    $('.modal').modal('hide');
                    $tab.reloadTab('#GroupManager');  
                }              
            }
        });
    });
</script>